package com.dingdayu.club.admin;

import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.dingdayu.club.admin.api.LoginApi;
import com.dingdayu.club.admin.common.util.HttpUtils;
import com.dingdayu.club.admin.common.util.SharePreferenceUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class SettingsActivity extends AppCompatActivity {

    public static TextView sTextView;
    public static EditText editText2;
    public static Spinner sSpinner;
    public static Button sButton;
    public static String name;
    public static String school;
    public static SharePreferenceUtil spU;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);

        sTextView = (TextView)findViewById(R.id.textView4);
        editText2 = (EditText)findViewById(R.id.editText2);
        sSpinner = (Spinner)findViewById(R.id.spinner);
        sButton = (Button)findViewById(R.id.button2);

        sButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                name = editText2.getText().toString();

                //提交数据
                saveSettingTask saveSettingTaskList = new saveSettingTask();
                saveSettingTaskList.execute("");
                //Snackbar.make(v, "设置保存成功", Snackbar.LENGTH_LONG).setAction("Action", null).show();
            }
        });

        //显示的数组
        final String arr[]=new String[]{
                "中州大学",
                "郑州师范",
        };

        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);

        /*List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("logo", R.drawable.ic_launcher);
        map.put("text","xxXx说明操作");

        Map<String, Object> map2=new HashMap<String, Object>();
        map2.put("logo", R.drawable.ic_launcher);
        map2.put("text","xxx说明xdds");

        data.add(map);
        data.add(map2);

        SimpleAdapter simpleAdapter=new SimpleAdapter(this, data, R.layout.activity_item, new String[]{"logo","text"}, new int[]{R.id.imageView1,R.id.textView1});*/
        //设置显示的数据
        sSpinner.setAdapter(arrayAdapter);
        //Toast.makeText(getApplicationContext(), "main Thread" + sSpinner.getItemIdAtPosition(sSpinner.getSelectedItemPosition()), sSpinner.LENGTH_LONG).show();

        //注册事件
        sSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int position, long id) {
                Spinner spinner=(Spinner) parent;
                school = spinner.getItemAtPosition(position).toString();
                //Toast.makeText(getApplicationContext(), "xxxx"+spinner.getItemAtPosition(position), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();
            }

        });
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * 保存设置
     */
    public class saveSettingTask extends AsyncTask<String, Integer, Boolean> {

        @Override
        protected Boolean doInBackground(String... params) {
            publishProgress(0);
            HashMap<String, String> hm = new HashMap<String, String>();

            try {
                HashMap<String, String> params2 = new HashMap<String, String>();
                //检查用户登录
                spU = new SharePreferenceUtil(LoginApi.USER_INFO_FILE);
                int ClubID = spU.getClubID();
                if(ClubID != 0){
                    //                String name = params[1];
//                String school = params[2];
                    Log.e("ClubID", String.valueOf(ClubID));
                    params2.put("club_id", String.valueOf(ClubID));
                    params2.put("name", name);
                    params2.put("school", school);
                    publishProgress(18);
                    String t = HttpUtils.submitPostData("http://corp.xyser.com/Home/Api/saveClubInfo", params2, "utf-8");
                    Log.e("t", t);
                    try {
                        publishProgress(80);
                        JSONObject jsonObj = new JSONObject(t);
                        int code = jsonObj.getInt("code");
                        String msg = jsonObj.getString("msg");
                        if (code == 302) {
                            // new AlertDialog.Builder(LoginActivity.loginactivity).setTitle(msg).setMessage("请重试").setPositiveButton("确定", null).show();
                            return false;
                        }

                        if (code == 200) {
                            return true;
                        }
                    }catch (JSONException e) {
                        //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
                        Log.e("LoginAPI", "返回数据解析错误" + e.getMessage());
                        //login_ret = false;
                        //e.printStackTrace();
                        return false;
                    }
                }

            } catch (Exception e) {
                return false;
            }
            publishProgress(100);
            return false;
        }

        protected void onProgressUpdate(Integer... progress) {
            //TtextView3.setText("loading..." + progress[0] + "%");
        }

        protected void onPostExecute(Boolean result) {
            if (result) {
                Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_LONG).show();
            } else {
                Log.i("Main","数据错误");
                Toast.makeText(getApplicationContext(), "保存失败", Toast.LENGTH_LONG).show();
            }
        }

        protected void onPreExecute() {
        }

        protected void onCancelled() {
            //mProgressBar.setProgress(0);
        }
    }
}
